/**
 * 
 */
package com.swg.cocobox.message;

import java.io.IOException;
import java.io.Serializable;

/**
 * @author satriaprayoga
 *
 */
public class MessagePackage implements Serializable {
	private static final long serialVersionUID = 1162182063300845971L;
	
	public static final String TYPE_PREFIX="type:";
	
	private byte[] messageData;
	private int type=0;
	
	public MessagePackage(byte[] msgData){
		this.messageData=msgData;
	}
	
	public MessagePackage(byte[] msgData,int type) {
		this.messageData=msgData;
		this.type=type;
	}
	
	public byte[] getMessageData() {
		return messageData;
	}
	
	public void setMessageData(byte[] messageData) {
		this.messageData = messageData;
	}
	
	public int getType() {
		if(type==0){
			type=MessagePackageUtil.getMessagetPackagetType(this);
		}
		return type;
	}
	
	public String getMessageDataString(String encoding) throws IOException{
		return new String(messageData,encoding);
	}
	
	public void removeType(){
		try {
			String msg=getMessageDataString("UTF-8");
			msg=msg.replaceFirst(MessagePackage.TYPE_PREFIX+"\\d{1}", "");
			msg=msg.trim();
			this.setMessageData(msg.getBytes("UTF-8"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
